package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PointsRule;
import com.ruoyi.system.service.IPointsRuleService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 积分规则Controller
 *
 * @author ruoyi
 * @date 2024-04-22
 */
@RestController
@RequestMapping("/system/pointsRule")
public class PointsRuleController extends BaseController
{
    @Autowired
    private IPointsRuleService pointsRuleService;

    /**
     * 查询积分规则列表
     */
    @PreAuthorize("@ss.hasPermi('system:rule:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody PointsRule pointsRule)
    {
        setStartPage(pointsRule.getPageNum(),pointsRule.getPageSize());
        List<PointsRule> list = pointsRuleService.selectPointsRuleList(pointsRule);
        return getDataTable(list);
    }

    /**
     * 获取积分规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:rule:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pointsRuleService.selectPointsRuleById(id));
    }

    /**
     * 新增积分规则
     */
    @PreAuthorize("@ss.hasPermi('system:rule:add')")
    @Log(title = "积分规则", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody PointsRule pointsRule)
    {
        pointsRule.setCreateBy(String.valueOf(getUserId()));
        pointsRule.setCreateName(getUsername());
        return pointsRuleService.insertPointsRule(pointsRule);
    }

    /**
     * 修改积分规则
     */
    @PreAuthorize("@ss.hasPermi('system:rule:edit')")
    @Log(title = "积分规则", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody PointsRule pointsRule)
    {
        return pointsRuleService.updatePointsRule(pointsRule);
    }

    /**
     * 删除积分规则
     */
    @PreAuthorize("@ss.hasPermi('system:rule:remove')")
    @Log(title = "积分规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(pointsRuleService.deletePointsRuleById(id));
    }
}
